<template>
  <div class="app-container">
    <div class="dataTable">
      <el-form :inline="true" label-position="right" label-width="64px" style="width: 100%; margin-top:0px;">
        <el-form-item label="单据日期:" prop="billDate">
          <el-date-picker v-model="form.billDate" type="date" placeholder="单据日期" size="small" :clearable="false" value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item label="单据号:" prop="billNo">
          <el-input v-model="form.billNo" size="small" placeholder="单据号" disabled />
        </el-form-item>
        <el-form-item label="客户:" prop="custId">
          <custList key-type="custId" :select-id="form.custId" @selectChange="selectChange" />
        </el-form-item>
        <el-form-item label="仓库:" prop="warehouseId">
          <warehouseList key-type="warehouseId" :select-id="form.warehouseId" @selectChange="selectChange" />
        </el-form-item>
        <el-form-item label="车辆:" prop="truckId">
          <truckList key-type="truckId" :select-id="form.truckId" @selectChange="selectChange" />
        </el-form-item>
        <el-form-item label="业务员:" prop="staffId">
          <staffList :select-id="form.staffId" @selectChange="selectChange" />
        </el-form-item>
        <el-form-item label="发票" prop="statusInvoice" class="invoice">
          <el-tooltip class="item invoice" effect="dark" content="不开发票，默认不计税；否则按默认税率计税" placement="bottom">
            <i class="el-icon-question" />
          </el-tooltip>
          <el-select v-model="form.statusInvoice" size="small">
            <el-option label="不开发票" :value="0" />
            <el-option label="待开发票" :value="1" />
            <el-option label="已开发票" :value="9" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="contentDiv">
      <el-table :data="tableData" border fit highlight-current-row style="width: 100%;" size="small" cell-class-name="tdCell">
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="商品" width="300">
          <template slot-scope="scope">
            <itemList :select-id="scope.row.itemId" :select-code="scope.row.itemCode" :index="scope.$index" @changeVal="changeVal" />
          </template>
        </el-table-column>
        <el-table-column label="规格">
          <template slot-scope="{row}">
            <input v-model="row.norms" type="text" class="inputCell" disabled />
          </template>
        </el-table-column>
        <el-table-column label="单位" width="60">
          <template slot-scope="{row}">
            <input v-model="row.uom" type="text" class="inputCell tx-c" disabled />
          </template>
        </el-table-column>
        <el-table-column label="批号">
          <template slot-scope="{row}">
            <input v-model="row.batchNo" type="text" class="inputCell" @focus="focusThis($event)" />
          </template>
        </el-table-column>
        <el-table-column label="生产日期" width="120">
          <template slot-scope="{row}">
            <el-date-picker v-model="row.productionDate" :editable="false" type="date" placeholder size="small" style="width:100%" :clearable="false" value-format="yyyy-MM-dd" />
          </template>
        </el-table-column>
        <el-table-column label="保质期(天)">
          <template slot-scope="{row}">
            <input v-model="row.qualityDays" type="text" class="inputCell tx-r" @focus="focusThis($event)" />
          </template>
        </el-table-column>
        <el-table-column label="含税价(元)">
          <template slot-scope="scope">
            <input v-model="scope.row.vatPrice" type="text" class="inputCell tx-r" @change="calculate(scope.$index)" @focus="focusThis($event)" />
          </template>
        </el-table-column>
        <el-table-column v-if="$route.path.indexOf('Split') > 0" label="原始数量">
          <template slot-scope="scope">
            <input v-model="scope.row.qty1" type="text" :index="scope.$index" class="inputCell tx-r" @change="calculate(scope.$index)" @focus="focusThis($event)" />
          </template>
        </el-table-column>
        <el-table-column label="数量">
          <template slot-scope="scope">
            <input v-model="scope.row.qty" type="text" :index="scope.$index" class="inputCell tx-r" @change="calculate(scope.$index)" @focus="focusThis($event)" />
          </template>
        </el-table-column>
        <el-table-column label="价税合计">
          <template slot-scope="{row}">
            <input v-model="row.vatAmount" type="text" class="inputCell tx-r" disabled />
          </template>
        </el-table-column>
        <el-table-column label="税率(%)">
          <template slot-scope="scope">
            <input v-model="scope.row.taxRate" type="text" class="inputCell tx-r" :disabled="taxType != 9" @focus="focusThis($event)" @change="calculate(scope.$index)" />
          </template>
        </el-table-column>
        <el-table-column label="税额">
          <template slot-scope="{row}">
            <input v-model="row.taxAmount" type="text" class="inputCell tx-r" disabled />
          </template>
        </el-table-column>
      </el-table>
      <div class="dataTable" style="margin-top: 10px">
        <el-form label-position="right" label-width="72px" style="width: 100%; margin-top:0px;">
          <el-form-item label="备 注:" prop="remarks">
            <el-input v-model="form.remarks" size="small" placeholder="备注" style="width:790px" />
          </el-form-item>
        </el-form>
        <el-form :inline="true" label-position="right" label-width="72px" style="width: 100%; margin-top:0px;">
          <el-form-item label="制单日期:" prop="recordDate">
            <el-date-picker v-model="form.recordDate" :editable="false" type="date" placeholder="制单日期" size="small" style="width:145px" :clearable="false" value-format="yyyy-MM-dd 00:00:00" />
          </el-form-item>
          <el-form-item label="制单人:" prop="recorderId">
            <el-input v-model="form.recorder" size="small" placeholder="制单人" @focus="focusThis($event)" />
          </el-form-item>
          <el-form-item label="审核日期:" prop="auditDate">
            <el-date-picker v-model="form.auditDate" :editable="false" type="date" placeholder="审核日期" size="small" style="width:145px" :clearable="false" value-format="yyyy-MM-dd 00:00:00" />
          </el-form-item>
          <el-form-item label="审核人:" prop="auditorId">
            <el-input v-model="form.auditor" size="small" placeholder="审核人" @focus="focusThis($event)" />
          </el-form-item>
        </el-form>
      </div>
      <div class="tx-c" style="margin-top:15px">
        <el-button v-if="$route.path.indexOf('etail') < 0" class="submitBtn" type="primary" @click="save">保 存</el-button>
        <el-button v-else class="submitBtn" type="primary" @click="$store.dispatch('tagsView/delView', $route);$router.replace('/store/outboundOrder')">返 回</el-button>
      </div>
    </div>
    <el-dialog :close-on-click-modal="false" title="选择拆分方式" :visible.sync="dialogVisible" width="400px">
      <el-form style="margin-top:30px;text-align:center;">
        <el-form-item label>
          <el-radio v-model="splitBillType" :label="1" style="margin-right:10px">拆分</el-radio>
          <el-radio v-model="splitBillType" :label="2">复制</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="default" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveAndFen">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { saveOutboundOrder, getOutboundOrderById } from '@/api/store'
import { deleteEmptyProp, addNullObj, initNullArray } from '@/utils'
import staffList from '@/components/selects/staffList'
import custList from '@/components/selects/custList'
import warehouseList from '@/components/selects/warehouseList'
import truckList from '@/components/selects/truckList'
import itemList from '@/components/selects/itemList'
import { getName, getNowDate } from '@/utils/auth'
const userInfo = JSON.parse(sessionStorage.userInfo)
export default {
  name: 'OutboundOrderAdd',
  components: { custList, staffList, warehouseList, truckList, itemList },
  data() {
    return {
      id: '',
      status: this.$route.query.status,
      taxFilingCategoryCode: userInfo.taxFilingCategoryCode,
      taxType: userInfo.book.taxType,
      isTaxRateEditable: userInfo.book.isTaxRateEditable || 0,
      tableData: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
      splitBillType: 1,
      dialogVisible: false,
      form: {
        remarks: '',
        billDate: getNowDate(),
        billNo: '',
        custId: '',
        bizTypeId: '',
        staffId: '',
        statusInvoice: 0,
        warehouseId: '',
        truckId: '',
        warehouseName: '',
        truckName: '',
        auditDate: '',
        auditor: '',
        recordDate: getNowDate() + ' 00:00:00',
        recorder: getName()
      }
    }
  },
  watch: {
    'form.statusInvoice': {
      handler(val, oldVal) {
        if (this.taxType == 2) {
          if (val > 0) {
            this.tableData.forEach(item => {
              this.$set(item, 'taxRate', userInfo.book.defaultTaxRate * 100)
            })
          } else {
            this.tableData.forEach(item => {
              this.$set(item, 'taxRate', 0)
            })
          }
        }
      },
      immediate: true
    }
  },
  created() {
    this.$store.dispatch('basedata/getitemlist')
    this.tableData = initNullArray()
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      getOutboundOrderById({ id: this.id, billDate: this.$route.query.billDate }).then(res => {
        if (res.data.data) {
          for (var key in this.form) {
            this.form[key] = res.data.data[key]
          }
          this.tableData = addNullObj(res.data.data.outboundOrderLine)
          for (var i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].taxRate < 1) {
              this.$set(this.tableData[i], 'taxRate', this.tableData[i].taxRate * 100)
            }
            if (this.$route.path.indexOf('Split') > 0) {
              this.$set(this.tableData[i], 'qty1', this.tableData[i].qty)
            }
          }
        }
      })
    }
  },
  methods: {
    focusThis(e) {
      e.currentTarget.select()
    },
    calculate(index) {
      var vatPrice = this.tableData[index].vatPrice // 含税价
      var qty = this.tableData[index].qty // 数量
      if (qty && vatPrice) {
        var vatAmount = parseFloat(Number(qty) * Number(vatPrice)).toFixed(2)
        this.$set(this.tableData[index], 'vatAmount', vatAmount)
        var taxRate = this.tableData[index].taxRate
        if (taxRate) {
          var amount = parseFloat(Number(vatAmount) / (Number(taxRate) / 100 + 1)).toFixed(2)
          var taxAmount = parseFloat(Number(vatAmount) - Number(amount)).toFixed(2)
          this.$set(this.tableData[index], 'taxAmount', taxAmount)
        } else {
          this.$set(this.tableData[index], 'taxAmount', 0)
        }
      }
    },
    selectChange(obj) {
      for (var key in obj) {
        this.form[key] = obj[key]
      }
      if (obj && obj.warehouseName) {
        this.form.warehouseName = obj.warehouseName
      }
      if (obj && obj.truckName) {
        this.form.truckName = obj.truckName
      }
    },
    changeVal(obj) {
      for (var key in obj) {
        this.$set(this.tableData[obj.index], key, obj[key])
      }
      if (obj.index + 1 === this.tableData.length) {
        this.tableData.push({})
        this.$nextTick(() => {
          const container = this.$el.querySelector('.el-table__body-wrapper')
          container.scrollTop = container.scrollHeight
        })
      }
    },
    save() {
      if (this.$route.path.indexOf('Split') > 0) {
        this.dialogVisible = true
        return
      }
      this.saveAndUpdate()
    },
    saveAndUpdate() {
      this.form.id = this.id
      this.form.outboundOrderLine = deleteEmptyProp(this.tableData)
      saveOutboundOrder(this.form).then(res => {
        if (res.data.errorCode === '0') {
          this.dialogVisible = false
          this.$message.success(res.data.msg)
          this.$store.dispatch('tagsView/delView', this.$route)
          this.$router.replace('/store/outboundOrder')
        }
      }).catch(() => {
        this.$message.error('保存失败，请稍后重试！')
      })
    },
    saveAndFen() {
      this.form.splitBillType = this.splitBillType
      this.saveAndUpdate()
    }
  }
}
</script>
